package com.yequan.designpattern.proxy.dynamic_proxy.cglib;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Auther: yq
 * @Date: 2018/9/5 09:59
 * @Description:
 */
public class CGLIBPersonProxy implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        return methodProxy.invokeSuper(method, objects);
    }

    public static Person getProxyInstance() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Person.class);
        enhancer.setCallback(new CGLIBPersonProxy());
        return (Person) enhancer.create();
    }

    public static void main(String[] args) {
        Person proxyInstance = CGLIBPersonProxy.getProxyInstance();
        proxyInstance.doSomething();
    }

}
